package com.tools.generator.service.impl;

import cn.hutool.core.util.StrUtil;
import com.tools.common.utils.DateUtils;
import com.tools.generator.entity.FieldTypeEntity;
import com.tools.generator.mapper.FieldTypeMapper;
import com.tools.generator.service.FieldTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class FieldTypeServiceImpl implements FieldTypeService {

    @Autowired
    private FieldTypeMapper fieldTypeMapper;

    @Override
    public List<FieldTypeEntity> page(FieldTypeEntity query) {
        return fieldTypeMapper.selectGenFieldTypeList(query);
    }

    @Override
    public Map<String, FieldTypeEntity> getMap() {
        List<FieldTypeEntity> list = fieldTypeMapper.selectGenFieldTypeList(null);
        Map<String, FieldTypeEntity> map = new LinkedHashMap<>(list.size());
        for (FieldTypeEntity entity : list) {
            map.put(entity.getColumnType().toLowerCase(), entity);
        }
        return map;
    }

    @Override
    public Set<String> getPackageByTableId(Long tableId) {
        Set<String> importList = fieldTypeMapper.getPackageByTableId(tableId);
        return importList.stream().filter(StrUtil::isNotBlank).collect(Collectors.toSet());
    }

    @Override
    public Set<String> getList() {
        return fieldTypeMapper.list();
    }

    @Override
    public int save(FieldTypeEntity entity) {
        entity.setAttrType(entity.getAttrType().trim());
        entity.setColumnType(entity.getColumnType().trim());
        entity.setPackageName(entity.getPackageName().trim());
        entity.setCreateTime(DateUtils.getNowDateTimeString());
        return fieldTypeMapper.insertGenFieldType(entity);
    }

    @Override
    public FieldTypeEntity getById(Long id) {
        return fieldTypeMapper.selectGenFieldTypeById(id);
    }

    @Override
    public void updateById(FieldTypeEntity entity) {
        entity.setAttrType(entity.getAttrType().trim());
        entity.setColumnType(entity.getColumnType().trim());
        entity.setPackageName(entity.getPackageName().trim());
        fieldTypeMapper.updateGenFieldType(entity);
    }

    @Override
    public void removeBatchByIds(List<Long> list) {
        fieldTypeMapper.deleteGenFieldTypeByIds(list);
    }
}
